package com.krtech.wecard.module.ramble.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.ramble.entity.RambleDrawChance;
import com.krtech.wecard.module.ramble.entity.RambleLeaderboardVo;
import com.krtech.wecard.module.ramble.entity.RambleLoginLog;
import com.krtech.wecard.module.ramble.enums.RambleEnums;
import com.krtech.wecard.module.ramble.mapper.RambleLoginLogMapper;
import com.krtech.wecard.module.ramble.service.RambleDrawChanceService;
import com.krtech.wecard.module.ramble.service.RambleLoginLogService;
import com.krtech.wecard.module.sys.entity.SysKeyValue;
import com.krtech.wecard.module.sys.entity.SysUser;
import com.krtech.wecard.module.sys.service.SysKeyValueService;
import com.krtech.wecard.module.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.krtech.wecard.module.ramble.entity.RambleLeaderboard;
import com.krtech.wecard.module.ramble.mapper.RambleLeaderboardMapper;
import com.krtech.wecard.module.ramble.service.RambleLeaderboardService;

@Slf4j
@Service
public class RambleLeaderboardServiceImpl extends ServiceImpl<RambleLeaderboardMapper, RambleLeaderboard> implements RambleLeaderboardService{


    @Autowired
    private RambleLoginLogService loginLogService;

    @Autowired
    private RambleLoginLogMapper loginLogMapper;

    @Autowired
    private RambleDrawChanceService drawChanceService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysKeyValueService keyValueService;


    @Override
    public boolean analyze(String phone) {
        //判断当前是否有，如果有，则更新
        RambleLeaderboard leaderboard = this.getOne(new QueryWrapper<RambleLeaderboard>().eq(RambleLeaderboard.COL_PHONE, phone));
        if (leaderboard == null){
            RambleLeaderboard rambleLeaderboard = new RambleLeaderboard();

            //计算全部步数
            int totalStep = 0;
            List<RambleLoginLog> loginLogList = loginLogService.list(new QueryWrapper<RambleLoginLog>().eq(RambleLoginLog.COL_PHONE, phone));
            if (loginLogList.size() > 0){
                int sumStep = loginLogList.stream().mapToInt(RambleLoginLog::getStepNumber).sum();
                totalStep = sumStep;
            }
            rambleLeaderboard.setTotalStep(totalStep);

            //计算昨天步数
            int yesterdayStep = 0;
            List<RambleLoginLog> yesterdayLoginLogList = loginLogService.list(new QueryWrapper<RambleLoginLog>().eq(RambleLoginLog.COL_PHONE, phone)
                    .eq(RambleLoginLog.COL_RIGISTER_DATE, DateUtil.parseDate(DateUtil.yesterday().toString())));
            if (yesterdayLoginLogList.size() > 0){
                int sumYesterdayStep = yesterdayLoginLogList.stream().mapToInt(RambleLoginLog::getStepNumber).sum();
                yesterdayStep = sumYesterdayStep;
            }
            rambleLeaderboard.setYesterdayStep(yesterdayStep);

            //查看抽奖次数
            RambleDrawChance drawChance = drawChanceService.getOne(new QueryWrapper<RambleDrawChance>().eq(RambleDrawChance.COL_PHONE, phone));
            rambleLeaderboard.setDrawChance(drawChance.getDrawChance());

            //放置用户信息
            SysUser user = userService.getOne(new QueryWrapper<SysUser>().eq(SysUser.COL_PHONE, phone));
            rambleLeaderboard.setUpdatedTime(new Date());
            rambleLeaderboard.setNickName(user.getNickName());
            rambleLeaderboard.setOpenId(user.getMipOpenId());
            rambleLeaderboard.setPhone(phone);
            log.info(phone+",排行榜更新完成");
            return this.save(rambleLeaderboard);
        }else {
            //计算全部步数
            int totalStep = 0;
            List<RambleLoginLog> loginLogList = loginLogService.list(new QueryWrapper<RambleLoginLog>().eq(RambleLoginLog.COL_PHONE, phone));
            if (loginLogList.size() > 0){
                int sumStep = loginLogList.stream().mapToInt(RambleLoginLog::getStepNumber).sum();
                totalStep = sumStep;
            }
            leaderboard.setTotalStep(totalStep);

            //计算昨天步数
            int yesterdayStep = 0;
            List<RambleLoginLog> yesterdayLoginLogList = loginLogService.list(new QueryWrapper<RambleLoginLog>().eq(RambleLoginLog.COL_PHONE, phone)
                    .eq(RambleLoginLog.COL_RIGISTER_DATE, DateUtil.parseDate(DateUtil.yesterday().toString())));
            if (yesterdayLoginLogList.size() > 0){
                int sumYesterdayStep = yesterdayLoginLogList.stream().mapToInt(RambleLoginLog::getStepNumber).sum();
                yesterdayStep = sumYesterdayStep;
            }
            leaderboard.setYesterdayStep(yesterdayStep);

            //查看抽奖次数
            RambleDrawChance drawChance = drawChanceService.getOne(new QueryWrapper<RambleDrawChance>().eq(RambleDrawChance.COL_PHONE, phone));
            leaderboard.setDrawChance(drawChance.getDrawChance());
            return this.updateById(leaderboard);
        }
    }

    @Override
    public RambleLeaderboardVo getData() {
        SysUser userInfo = LoginUtil.getUserInfo();
        String phone = userInfo.getPhone();

        //查询环境变量
        SysKeyValue stepKeyValue = keyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, RambleEnums.STEP));

        if (stepKeyValue == null){
            throw new BusinessException("环境变量step未配置，请联系管理员");
        }

        int step = Integer.parseInt(stepKeyValue.getSysValue());


        loginLogService.generateDrawChance();

        RambleLeaderboard leaderboard = this.getOne(new QueryWrapper<RambleLeaderboard>().eq(RambleLeaderboard.COL_PHONE, userInfo.getPhone()));
        if (leaderboard == null){
            RambleLeaderboard rambleLeaderboard = new RambleLeaderboard();

            //查看抽奖次数
            RambleDrawChance drawChance = drawChanceService.getOne(new QueryWrapper<RambleDrawChance>().eq(RambleDrawChance.COL_PHONE, phone));
            rambleLeaderboard.setDrawChance(drawChance.getDrawChance());


            //计算全部步数
            int totalStep = 0;
            List<RambleLoginLog> loginLogList = loginLogService.list(new QueryWrapper<RambleLoginLog>().eq(RambleLoginLog.COL_PHONE, phone));
            if (loginLogList.size() > 0){
                int sumStep = loginLogList.stream().mapToInt(RambleLoginLog::getStepNumber).sum();
                totalStep = sumStep;

                //判断抽奖次数
                int chance = sumStep / step;
                if (rambleLeaderboard.getDrawChance() < chance){
                    rambleLeaderboard.setDrawChance(chance);
                }
            }
            rambleLeaderboard.setTotalStep(totalStep);

            //计算昨天步数
            int yesterdayStep = 0;
            List<RambleLoginLog> yesterdayLoginLogList = loginLogService.list(new QueryWrapper<RambleLoginLog>().eq(RambleLoginLog.COL_PHONE, phone)
                    .eq(RambleLoginLog.COL_RIGISTER_DATE, DateUtil.parseDate(DateUtil.yesterday().toString())));
            if (yesterdayLoginLogList.size() > 0){
                int sumYesterdayStep = yesterdayLoginLogList.stream().mapToInt(RambleLoginLog::getStepNumber).sum();
                yesterdayStep = sumYesterdayStep;
            }

            rambleLeaderboard.setYesterdayStep(yesterdayStep);


            //放置用户信息
            SysUser user = userService.getOne(new QueryWrapper<SysUser>().eq(SysUser.COL_PHONE, phone));
            rambleLeaderboard.setUpdatedTime(new Date());
            rambleLeaderboard.setNickName(user.getNickName());
            rambleLeaderboard.setOpenId(user.getMipOpenId());
            rambleLeaderboard.setPhone(phone);
            log.info(phone+",排行榜更新完成");
            this.save(rambleLeaderboard);

            leaderboard = rambleLeaderboard;
        }else {
            //计算全部步数
            //查看抽奖次数
            RambleDrawChance drawChance = drawChanceService.getOne(new QueryWrapper<RambleDrawChance>().eq(RambleDrawChance.COL_PHONE, phone));
            leaderboard.setDrawChance(drawChance.getDrawChance());

            int totalStep = 0;
            List<RambleLoginLog> loginLogList = loginLogService.list(new QueryWrapper<RambleLoginLog>().eq(RambleLoginLog.COL_PHONE, phone));
            if (loginLogList.size() > 0){
                int sumStep = loginLogList.stream().mapToInt(RambleLoginLog::getStepNumber).sum();
                totalStep = sumStep;

                //判断抽奖次数
                int chance = sumStep / step;
                if (leaderboard.getDrawChance() < chance){
                    leaderboard.setDrawChance(chance);
                }
            }
            leaderboard.setTotalStep(totalStep);

            //计算昨天步数
            int yesterdayStep = 0;
            List<RambleLoginLog> yesterdayLoginLogList = loginLogService.list(new QueryWrapper<RambleLoginLog>().eq(RambleLoginLog.COL_PHONE, phone)
                    .eq(RambleLoginLog.COL_RIGISTER_DATE, DateUtil.parseDate(DateUtil.yesterday().toString())));
            if (yesterdayLoginLogList.size() > 0){
                int sumYesterdayStep = yesterdayLoginLogList.stream().mapToInt(RambleLoginLog::getStepNumber).sum();
                yesterdayStep = sumYesterdayStep;
            }
            leaderboard.setYesterdayStep(yesterdayStep);


            this.updateById(leaderboard);
        }

        RambleDrawChance drawChance = drawChanceService.getOne(new QueryWrapper<RambleDrawChance>().eq(RambleDrawChance.COL_PHONE, userInfo.getPhone()));

        RambleLeaderboardVo leaderboardVo = new RambleLeaderboardVo();
        BeanUtil.copyProperties(leaderboard,leaderboardVo);
        leaderboardVo.setHexiaoCode(drawChance.getHexiaoCode());
        leaderboardVo.setHexiaoUrl(drawChance.getHexiaoUrl());
        return leaderboardVo;
    }
}
